package com.guqj.nio.netty.serialization.jdk;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import com.guqj.nio.netty.serialization.jdk.pojo.SubscribeReq;

public class SubscribeClientHandler extends SimpleChannelInboundHandler<Object> {

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {

	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("Receive server response: [" + msg + "]");
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		for (int i = 1; i <= 10; i++) {
			ctx.writeAndFlush(createReq(i));
		}
	}

	private SubscribeReq createReq(int i) {
		SubscribeReq req = new SubscribeReq();
		req.setSubReqID(i);
		req.setProductName("Netty 权威指南");
		req.setUserName("LiLei");
		req.setPhoneNum("15865662222");
		req.setAddress("上海");
		return req;
	}
}
